<template>
  <div class="setting">
    <Card :dis-hover="true">
      <Form ref="form_data" :model="form_data" :rules="rule_form_data">
        <FormItem label="余额配置(使用模块独立余额或者微擎通用余额)" prop="use_balance">
          <br />
          <RadioGroup v-model="form_data.use_balance">
            <Radio label="1">独立余额</Radio>
            <Radio label="2">通用余额</Radio>
          </RadioGroup>
        </FormItem>
        <FormItem label="是否支持余额发红包" prop="balance_send">
          <i-switch v-model="form_data.balance_send" />
        </FormItem>
        <FormItem label="最低提现金额" prop="low_deposit">
          <Input v-model.number="form_data.low_deposit" placeholder="最低提现金额"></Input>
        </FormItem>
        <FormItem label="提现金额大于多少需审核(0.00=所有提现都需要审核)" prop="deposit_check">
          <Input v-model.number="form_data.deposit_check" placeholder="提现金额大于多少需审核(0.00=所有提现都需要审核)"></Input>
        </FormItem>
        <FormItem label="提现成功备注信息" prop="remark">
          <Input v-model.number="form_data.remark" placeholder="提现成功备注信息"></Input>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="submit('form_data')">确定</Button>
        </FormItem>
      </Form>
    </Card>
  </div>
</template>

<script>
export default {
  name: 'finance-setting',
  data () {
    const validateMoney = (rule, value, callback) => {
      if (!/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(value)) {
        callback(new Error('请输入正确的金额'))
      } else {
        callback()
      }
    }
    return {
      form_data: {
        use_balance: '1',
        balance_send: false,
        low_deposit: '0.00',
        deposit_check: '0.00',
        remark: ''
      },
      rule_form_data: {
        low_deposit: [
          { validator: validateMoney, trigger: 'blur' }
        ],
        deposit_check: [
          { validator: validateMoney, trigger: 'blur' }
        ],
        remark: [
          { max: 20, message: '备注信息字数不能大于20', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    data () {
      let { use_balance, balance_send, low_deposit, deposit_check, remark } = this.form_data
      return {
        use_balance,
        balance_send: balance_send ? 1 : 0,
        low_deposit,
        deposit_check,
        remark
      }
    }
  },
  mounted () {
    this.find()
  },
  methods: {
    // Run
    /**
     * 获取参数
     */
    find () {
      this.$get('settingFinance/find')
        .then((res) => {
          let { code, data } = res.data
          if (code === 0) {
            let { use_balance, balance_send, low_deposit, deposit_check, remark } = data
            this.form_data = {
              use_balance,
              balance_send: balance_send === '1',
              low_deposit,
              deposit_check,
              remark
            }
          }
        })
    },

    // Handle
    /**
     * 提交设置参数
     */
    submit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.$post('settingFinance/set', {
            data: this.data
          })
            .then((res) => {
              if (res.data.code === 0) {
                this.$Message.success('设置成功')
              } else {
                this.$Message.error('设置失败')
              }
            })
        }
      })
    }
  }
}
</script>
